package com.zyf.controller;


import com.zyf.pojo.SmbmsBill;
import com.zyf.pojo.SmbmsProvider;
import com.zyf.pojo.BilllAndProviderVo;
import com.zyf.service.SmbmsBillService;
import com.zyf.service.SmbmsProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张忆菲
 * @since 2020-09-09
 */
@Controller
public class SmbmsBillController {

    @Resource
    SmbmsBillService service;

    @Resource
    SmbmsProviderService providerService;


    @GetMapping("/bill/findAll")
    public String findAll(Model model){
        List<BilllAndProviderVo> bills = service.findAll();
        model.addAttribute("bills",bills);
        return "bill/billList";
    }

    @PostMapping("/bill/findAllLike")
    public String findAllLike(BilllAndProviderVo vo,Model model){
        List<BilllAndProviderVo> like = service.findAllLike(vo);
        model.addAttribute("bills",like);
        return "bill/billList";
    }

    @GetMapping("/bill/toAdd")
    public String toAdd(Model model){
        List<SmbmsProvider> list = providerService.list(null);
        model.addAttribute("providers",list);
        return "bill/billAdd";
    }
    @PostMapping("/bill/add")
    public String add(SmbmsBill bill){
        boolean save = service.save(bill);
        if (save==false){
            return "bill/billAdd";
        }
        return "redirect:/bill/findAll";
    }

    @GetMapping("/bill/toUpdate/{billId}")
    public String toUpdate(@PathVariable("billId") Integer billId,Model model){
        SmbmsBill bill = service.getById(billId);
        model.addAttribute("bill",bill);
        List<SmbmsProvider> providers = providerService.list(null);
        model.addAttribute("providers",providers);
        return "bill/billUpdate";
    }

    @PostMapping("/bill/update")
    public String update(SmbmsBill bill,Model model){
        boolean b = service.updateById(bill);
        if(b==false){
            model.addAttribute("message","修改失败");
            return "bill/billUpdate";
        }
        BilllAndProviderVo allVo = service.findAllVo(bill.getBillId());
        model.addAttribute("bill",allVo);
        return "bill/billView";
    }

    @PostMapping("/bill/delete")
    public String delete(SmbmsBill bill,Model model){
        boolean b = service.removeById(bill.getBillId());
        if(b==false){
            model.addAttribute("message","删除失败");
            return "bill/billView";
        }
        return "redirect:/bill/findAll";
    }
}

